﻿import {
  Component,
  Injector,
  ViewChild,
  ViewEncapsulation,
  OnInit,
  AfterViewInit,
} from "@angular/core";
import { appModuleAnimation } from "@shared/animations/routerTransition";
import { AppComponentBase } from "@shared/common/app-component-base";
import {
  PipelineServiceProxy,
  PipelineAndActsListDto,
} from "@shared/service-proxies/service-proxies";
import { Paginator } from "primeng/components/paginator/paginator";
import { LazyLoadEvent } from "primeng/primeng";
import { createPipeInstance } from "@angular/core/src/view/provider";

@Component({
  templateUrl: "./flex.component.html",
  styleUrls: ["./flex.component.less"],
  encapsulation: ViewEncapsulation.None,
  animations: [appModuleAnimation()],
})
export class FlexsComponent extends AppComponentBase implements OnInit {
  @ViewChild("paginator") paginator: Paginator;

  filterText = "";
  isFinished = false;
  isFinishedName = "进行";

  pipes = new Array<PipelineAndActsListDto>();

  constructor(
    injector: Injector,
    private _pipelineService: PipelineServiceProxy
  ) {
    super(injector);
  }

  ngOnInit(): void {
    this.getPipes();
  }

  getDoingPipeines() {
    if (!this.isFinished) {
      return;
    }

    this.isFinished = false;
    this.isFinishedName = "进行";
    this.getPipes();
  }

  getFilishedPipeines() {
    if (this.isFinished) {
      return;
    }

    this.isFinished = true;
    this.isFinishedName = "完成";
    this.getPipes();
  }

  getPipes(event?: LazyLoadEvent) {
    if (this.primengTableHelper.shouldResetPaging(event)) {
      this.paginator.changePage(0);
      return;
    }

    this.primengTableHelper.showLoadingIndicator();
    if (parseInt(this.paginator._rows.toString()) === 0) {
      this.paginator._rows = 5;
    }
    this._pipelineService
      .getPipelinesAndActs(
        this.filterText,
        1,
        "51",
        undefined,
        this.isFinishedName,
        undefined,
        parseInt(this.paginator._rows.toString()),
        this.primengTableHelper.getSkipCount(this.paginator, event)
      )
      .subscribe((result) => {
        this.primengTableHelper.totalRecordsCount = result.totalCount;
        this.pipes = result.items;
        this.primengTableHelper.hideLoadingIndicator();
      });
  }
}
